/**
 * @param {number[]} nums
 * @return {number}
*/
var majorityElement = function(nums) {
  if(nums.length == 1) return nums[0];
  if(nums.length == 0) return -1;
  nums.sort((a, b) => a - b);
  let num = nums[0], count = 1;
  for(let i = 1; i < nums.length; i++) {
    if(nums[i] == num) {
      count++;
      if(count > nums.length / 2) {
        return num;
      }
    } else {
      num = nums[i];
      count = 1;
    }
  }
  return -1;
};
let nums = [1,2,5,9,5,9,5,5,5];
console.log(majorityElement(nums));
nums = [3,2];
console.log(majorityElement(nums));
nums = [2,2,1,1,1,2,2];
console.log(majorityElement(nums));
